package com.qys.training.biz.auth.service.impl;

import com.qys.training.base.enumerate.BizCodeEnum;
import com.qys.training.base.exception.QysException;
import com.qys.training.base.util.BaseUtils;
import com.qys.training.biz.auth.criteria.UserCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qys.training.biz.auth.entity.User;
import com.qys.training.biz.auth.mapper.UserMapper;
import com.qys.training.biz.auth.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService{

	public static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper userMapper;

	public Long create(User user) {
		if (userMapper.findByUsername(user.getUsername()) != null) {
			throw new QysException(BizCodeEnum.ERROR.getCode(),"用户名已存在");
		}
		user.setPassword(BaseUtils.stringToMD5(user.getPassword()));
		userMapper.insert(user);
		return user.getId();
	}


	public User get(Long id) {
		return userMapper.findById(id);
	}
	public User findByCriteria(UserCriteria criteria) {
		return userMapper.findByCriteria(criteria);
	}

}
